define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Tab_cart, Auth, GoPage, $timeout, GoDialog, $ionicModal) {
		$scope.input = new Object();
		$scope.input.is_all = false;
		$scope.input.number = 0;
		
		//判断是否登录
		$scope.$on('$ionicView.beforeEnter', function(scope, status) {
			//未登录时，跳转到登录
			if(!Auth.check_loginInfo()) {
				GoPage.go_states("t-login");
			}
		}, false);
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.init_data();
		}, false);
		
		$scope.init_data = function() {
			$scope.firstHide = false;//避免闪现“没有数据”
			Tab_cart.get_cart(function(data) {
				if(data != null) {
					$scope.datas = data;
					if(data.store != undefined) {
						$scope.get_cost(data);
						$scope.init_store_check();
						$scope.is_select_all(data);
						$scope.init_promotion(data);
					}
				} else{
					$scope.datas = new Object();
				}
				$scope.firstHide = true;
			})
		}
		
		$scope.init_promotion = function(data) {
			for(var i = 0; i < data.store.length; i++) {
				if(data.store[i].discount != null) {
					data.store[i].discount_selected = true;
					data.store[i].coupon_selected = false;
				} else {
					data.store[i].coupon_selected = true;
					data.store[i].discount_selected = false;
				}
			}
		}
		
		//删除商品
		$scope.delete_goods = function($event, ident_id, type, group_id, product_id) {
			$event.stopPropagation();//阻止事件冒泡
			GoDialog.confirm("删除商品","确定要删除吗？",function(){
				var obj = '';
				if(type == 'adjunct') {
					var obj = new Array();
					obj[0] = {
						"group_id": group_id,
						"product_id": product_id
					}
					obj = JSON.stringify(obj);
				}
				Tab_cart.delete_goods(ident_id, type, obj, function(data) {
					if(data.success == true) {
						if(data.result.data) {
							$scope.get_cost(data.result.data);
							for(var i = 0; i < $scope.datas.store.length; i++) {
								for(var j = 0; j < data.result.data.store.length; j++) {
									if($scope.datas.store[i].id == data.result.data.store[j].id) {
										data.result.data.store[j].discount_selected = $scope.datas.store[i].discount_selected;
										data.result.data.store[j].coupon_selected = $scope.datas.store[i].coupon_selected;
										break;
									}
								}
							}
							$scope.datas.store = data.result.data.store;

							$scope.init_store_check();
							$scope.is_select_all();
							$scope.datas.total_amount = data.result.promotion_subtotal;
						} else {
							$scope.datas.store = null;
							$scope.datas.total_amount = 0;
							$scope.input.number = 0;
							if(data.result.like_list){
								//猜你喜欢
								$scope.datas.like_list = data.result.like_list;
							}
						}
					} else
						GoDialog.toast(data.msg);
				})
			})
		}
		
		$scope.is_select_all = function() {
			for(var i = 0; i < $scope.datas.store.length; i++) {
				if($scope.datas.store[i].is_invalid != true) {
					$scope.datas.store[i].choose = true;
					for(var j = 0; j < $scope.datas.store[i].products.length; j++) {
						if($scope.datas.store[i].products[j].choose == 0 && $scope.datas.store[i].products[j].invalid_flg != true) {
							$scope.datas.store[i].choose = false;
							break;
						}
					}
				}
			}
			for(var i = 0; i < $scope.datas.store.length; i++) {
				if($scope.datas.store[i].is_invalid != true) {
					for(var j = 0; j < $scope.datas.store[i].products.length; j++) {
						if($scope.datas.store[i].products[j].choose == 0 && $scope.datas.store[i].products[j].invalid_flg != true) {
							$scope.input.is_all = false;
							return;
						}
					}
				}
			}
			$scope.input.is_all = true;
		}
		
		//全选
		$scope.select_all = function() {
			if(!$scope.datas||!$scope.datas.store){
				GoDialog.toast('请先加购商品！');
				return;
			}
			var value;
			if($scope.input.is_all) {//取消选中
				$scope.input.is_all = false;
				value = 2;
			} else {//选中
				$scope.input.is_all = true;
				value = 1;
			}
			Tab_cart.goods_choose_all(value, function(data) {
				if(data.success == true) {
					$scope.get_cost(data.result.data);
					for(var i = 0; i < $scope.datas.store.length; i++) {
						data.result.data.store[i].discount_selected = $scope.datas.store[i].discount_selected;
						data.result.data.store[i].coupon_selected = $scope.datas.store[i].coupon_selected;
					}
					$scope.datas.store = data.result.data.store;
					$scope.init_store_check();
					$scope.is_select_all();
					$scope.datas.total_amount = data.result.promotion_subtotal;
				} else
					GoDialog.toast(data.msg);
			})
		}
		
		/**
		 * 选择单个商品
		 * storeIndex：外层index
		 * index：内层index，即products的下标
		 */
		$scope.goods_select = function($event, type, ident_id, value, index) {
			$event.stopPropagation();//阻止事件冒泡
			if(value){//取消选中
				value = 2;
			}else{//选中
				value = 1;
			}
			Tab_cart.goods_choose(type, ident_id, value, function(data) {
				if(data.success == true) {
					$scope.datas.store[index] = $scope.store_check(data, index);
					$scope.datas.total_amount = data.result.promotion_subtotal;
					$scope.input.number = data.result.quantity;
					$scope.is_select_all();
				} else {
					GoDialog.toast(data.msg);
				}
			})
		}
		
		$scope.store_check = function(data, index) {
			data.result.data.store.discount_selected = $scope.datas.store[index].discount_selected;
			data.result.data.store.coupon_selected = $scope.datas.store[index].coupon_selected;
			for(var i = 0; i < data.result.data.store.products.length; i++) {
				if(data.result.data.store.products[i].invalid_flg == false) {
					data.result.data.store.is_invalid = false;
					if(data.result.data.store.products[i].choose == 1) {
						data.result.data.store.choose = true;
					} else {
						data.result.data.store.choose = false;
					}
				} else {
					data.result.data.store.is_invalid = true;
					break;
				}
			}
			return data.result.data.store;
		}
		
		$scope.init_store_check = function() {
			for(var i = 0; i < $scope.datas.store.length; i++) {
				for(var j = 0; j < $scope.datas.store[i].products.length; j++) {
					if($scope.datas.store[i].products[j].invalid_flg == false) {
						$scope.datas.store[i].is_invalid = false;
					} else
						$scope.datas.store[i].is_invalid = true;
					break;
				}
			}
		}
		
		//选择单个店铺
		$scope.store_select = function($event, store_id, value, index) {
			$event.stopPropagation();//阻止事件冒泡
			if(value){//取消选中
				value = 2;
			}else{//选中
				value = 1;
			}
			Tab_cart.store_choose(store_id, value, function(data) {
				if(data.success == true) {
					$scope.datas.store[index] = $scope.store_check(data, index);
					$scope.datas.total_amount = data.result.promotion_subtotal;
					$scope.input.number = data.result.quantity;
					$scope.is_select_all();
				} else
					GoDialog.toast(data.msg);
			})
		}
		
		//商品数量+1/-1						
		$scope.addAndReduceGoods = function($event, num, ident_id, type, buy_base, index, group_id, product_id, isAdd) {
			$event.stopPropagation();//阻止事件冒泡
			var count = 1;
			if(buy_base != null && buy_base != '1' && buy_base != '0') {//最小购买基数
				count = parseInt(buy_base);
			}
			num = parseInt(num);//一定要强转
			if(isAdd){//添加
				num = num + count;
			}else{
				if(num == 0) return;
				num = num - count;
			}
			var product ;
			if(type == 'adjunct') {//配件
				product = new Array();
				product[0] = {
					"group_id": group_id,
					"product_id": product_id,
					"quantity": num
				}
			} else {
				product = new Object();
				product = {
					"quantity": num
				}
			}
			Tab_cart.update_item(ident_id, type, product, function(data) {
				if(data.success == true) {
					//if(data.msg == '采购车修改成功！') {
					$scope.datas.store[index] = $scope.store_check(data, index);
					//} else {
					//	GoDialog.toast(data.msg);
					//}
					$scope.datas.total_amount = data.result.promotion_subtotal;
					$scope.input.number = data.result.quantity;
				} else
					GoDialog.toast(data.msg);
			})
		}
		
		$scope.get_cost = function(data) {
			$scope.input.cost = 0;
			$scope.input.number = 0;
			for(var i = 0; i < data.store.length; i++) {
				for(var j = 0; j < data.store[i].products.length; j++) {
					if(data.store[i].products[j].choose == 1) {
						$scope.input.number += parseInt(data.store[i].products[j].num);
						$scope.input.cost += parseInt(data.store[i].products[j].num) * parseFloat(data.store[i].products[j].price);
						if(data.store[i].products[j].suit != undefined) {
							for(var n = 0; n < data.store[i].products[j].suit.length; n++) {
								$scope.input.number += parseInt(data.store[i].products[j].suit[n].num);
								$scope.input.cost += parseInt(data.store[i].products[j].suit[n].num) * parseFloat(data.store[i].products[j].suit[n].price);
							}
						}
					}
				}
			}
		}

		//结算，提交
		$scope.onshow_afsrue = function() {
			var isCheck = false;
			if(!$scope.datas||!$scope.datas.store){
				GoDialog.toast('请先加购商品！');
				return;
			}
			var data = $scope.datas.store;
			outSideFor:
			for(var i = 0; i < data.length; i++) {
				for(var j = 0; j < data[i].products.length; j++) {
					if(data[i].products[j].choose){
						isCheck = true;
						break outSideFor;//跳出最外层循环
					}
				}
			}
			if(isCheck)
				GoPage.go_states("afsrue");
			else
				GoDialog.toast('请先选择商品！');
		};
		
		
		//页面跳转，跳转到“全部商品”
		$scope.showStore = function(store_id, index) {
			GoPage.go_states("first", {
				'store_id': store_id,
				'index': index
			});
		};
		$scope.show_goods = function(goods_id, type) {
			GoPage.go_states("productdetail", {
				'goods_id': goods_id,
				'type': type
			});
		};
		$scope.go_home = function() {
			GoPage.go_tabPage('home');
		}
		
		/***************领取优惠券**************/
		$ionicModal.fromTemplateUrl("templates/modal/couponListModal.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_coupon = modal;
		});
		//打开。index为store的下标
		$scope.openModal_coupon = function($event,index) {
			$event.stopPropagation();//阻止事件冒泡
			var data = $scope.datas.store[index];
			if(!data.coupon) return;
			$scope.detail = new Object();
			$scope.detail.store_name = data.name;
			$scope.detail.store_coupons = data.coupon;
			$scope.modal_coupon.show();
		}
		//隐藏
		$scope.closeModal_coupon = function() {
			$scope.modal_coupon.hide();
		}
		//领取优惠券
		$scope.get_coupon = function(cpns_id){
			Tab_cart.get_coupon(cpns_id, function(data) {
				if(data.success) {
					GoDialog.toast("领取优惠券成功！");
				} else{
					GoDialog.toast(data.msg);
				}
			})
		}
		/***************领取优惠券**************/
		

	}

	controller.$inject = ['$scope', 'Tab_cart', 'Auth', 'GoPage', '$timeout', 'GoDialog', '$ionicModal'];

	return app.registerController("tab_cartctrl", controller);
});